<?php

/**
 * Shopalong split page results class
 *
 * $Id: split_page_results.php,v 1.15 2003/06/09 22:35:34 hpdl Exp $
 * Copyright (c) 2003 osCommerce
 * Portions Copyright (c) 2006 Shopalong
 *
 * Released under the GNU General Public License
 */

class SplitPageResults
{
	var $_currentPageNumber;
	var $_numberOfPages;
	var $_pageName;
	var $_resultsClass = null;
	var $_resultsPerPage;
	var $_sqlQuery;
	var $_totalResults;
	
	/**
	 * Constructor function
	 *
	 * @access public
	 */
	
	function SplitPageResults($query, $page=1, $show=MAX_DISPLAY_PAGE_LINKS, $countKey = '*')
	{
		global $HTTP_GET_VARS, $HTTP_POST_VARS;
		
		$page = (is_numeric($page) && $page > 0) ? $page : '1';
		$show = (is_numeric($show) && $show > 0) ? $show : '1';
		$this->_sqlQuery = $query;
		$this->_currentPageNumber = $page;
		$this->_resultsPerPage = $show;
		$pos_to = strlen($this->_sqlQuery);
		$pos_from = strpos($this->_sqlQuery, ' from', 0);
		$pos_group_by = strpos($this->_sqlQuery, ' group by', $pos_from);
		
		if (($pos_group_by < $pos_to) && ($pos_group_by != false))
		{
			$pos_to = $pos_group_by;
		}
		
		$pos_having = strpos($this->_sqlQuery, ' having', $pos_from);
		
		if (($pos_having < $pos_to) && ($pos_having != false))
		{
			$pos_to = $pos_having;
		}
		
		$pos_order_by = strpos($this->_sqlQuery, ' order by', $pos_from);
		
		if (($pos_order_by < $pos_to) && ($pos_order_by != false))
		{
			$pos_to = $pos_order_by;
		}
		
		if (strpos($this->_sqlQuery, 'distinct') || strpos($this->_sqlQuery, 'group by'))
		{
			$count_string = 'distinct ' . tep_db_input($countKey);
		}
		
		else
		{
			$count_string = tep_db_input($countKey);
		}
		
		$count_query = tep_db_query('select count('.$count_string.') as total '.substr($this->_sqlQuery, $pos_from, ($pos_to - $pos_from)));
		$count = tep_db_fetch_array($count_query);
		$this->_totalResults = $count['total'];
		$this->_numberOfPages = ceil($this->_totalResults / $this->_resultsPerPage);
		
		if ($this->_currentPageNumber > $this->_numberOfPages)
		{
			$this->_currentPageNumber = $this->_numberOfPages;
		}
		
		$offset = ($this->_resultsPerPage * ($this->_currentPageNumber - 1));
		$this->_sqlQuery .= ' limit '.$offset.', '.$this->_resultsPerPage;
	}
	
	/**
	 * Get the current page number
	 *
	 * @access public
	 * @return integer Page number
	 */
	
	function getCurrentPage()
	{
		return $this->_currentPageNumber;
	}
	
	/**
	 * Get the first index of the split eg: From ...
	 *
	 * @access public
	 * @return integer First index
	 */
	
	function getFirstIndex()
	{
		$from_num = ($this->_resultsPerPage * ($this->_currentPageNumber - 1));
		$to_num = $this->getLastIndex();
		
		if ($to_num == 0)
		{
			$from_num = 0;
		}
		
		else
		{
			$from_num++;
		}
		
		return $from_num;
	}
	
	/**
	 * Get the last index of the split eg: To ...
	 *
	 * @access public
	 * @return integer Last index
	 */
	
	function getLastIndex()
	{
		$to_num = ($this->_resultsPerPage * $this->_currentPageNumber);
		
		if ($to_num > $this->_totalResults)
		{
			$to_num = $this->_totalResults;
		}
		
		return $to_num;
	}
	
	/**
	 * Get the results of the split
	 *
	 * @access public
	 */
	
	function getResults()
	{
		$results = array();
		$queryResults = tep_db_query($this->_sqlQuery);
		
		while ($result = tep_db_fetch_array($queryResults))
		{
			if (tep_not_null($this->_resultsClass) && class_exists(get_class($this->_resultsClass)))
			{
				$class = get_class($this->_resultsClass);
				$result = new $class($result);
			}
			
			array_push($results, $result);
		}
		
		return $results;
	}
	
	/**
	 * Get the total number of pages to show the results
	 *
	 * @access public
	 * @return integer
	 */
	
	function getTotalPages()
	{
		return $this->_numberOfPages;
	}
	
	/**
	 * Get the total number of results found
	 *
	 * @access public
	 * @return integer
	 */
	
	function getTotalResults()
	{
		return $this->_totalResults;
	}
	
	/**
	 * Is the specified page number the current page?
	 *
	 * @access public
	 * @param integer $pageNum The number to test against
	 * @return boolean Is it the current page number?
	 */
	
	function isCurrentPage($pageNum)
	{
		return ($pageNum == $this->_currentPageNumber);
	}
	
	/**
	 * Set the class to be used as the returned results object
	 *
	 * @access public
	 * @param object $class Must accept an array as the only required
	 * constructor arg
	 */
	
	function setResultsClass($class)
	{
		if (is_object($class))
		{
			$this->_resultsClass = $class;
		}
	}
}

?>
